﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System.Collections.Generic;
using System.Linq;
using DotNet.JCore.Entity;
using DotNet.Collections;
using DotNet.Extensions;
using DotNet.Utility;
using DotNet.Helper;
using System;
using DotNet.Data;

namespace DotNet.JCore.Service
{
    /// <summary>
    /// 人员信息管理服务
    /// </summary>
    public class EmployeeService
    {
        private JCoreRepository<Employee> repos = new JCoreRepository<Employee>();

        /// <summary>
        /// 是否存在指定名称的对象
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="name">名称</param>
        /// <returns>如果存在返回false</returns>
        public BoolMessage ExistsByName(string id, string name)
        {
            if (repos.Exists(p => p.Name == name && p.Id != id))
            {
                return new BoolMessage(false, $"已经存在姓名为: {name} 的人员");
            }
            return BoolMessage.True;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="isCreate">是否新增</param>
        public BoolMessage Save(Employee entity, bool isCreate)
        {
            var result = EntityHelper.ValidAsMessage(entity, "<br>");
            if (result.Failure)
            {
                return result;
            }
            result = ExistsByName(entity.Id, entity.Name);
            if (result.Failure)
            {
                return result;
            }

            if (isCreate)
            {
                repos.Insert(entity);
            }
            else
            {
                repos.UpdateExclude(entity, p => p.CreateId, p => p.CreateName, p => p.CreateTime);
            }
            return BoolMessage.True;
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="ids">主键数组</param>
        public BoolMessage Delete(string[] ids)
        {
            repos.Delete(ids);
            return BoolMessage.True;
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="id">主键</param>
        public Employee Get(string id)
        {
            return repos.Get(id);
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="userId">账号主键</param>
        public Employee GetByUserId(string userId)
        {
            return repos.Get(p => p.UserId == userId);
        }
        public Employee GetByUserInspectorsName(string InspectorsName)
        {
            return repos.Get(p => p.UserAccount == InspectorsName.Trim());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="PointId"></param>
        /// <returns></returns>
        public List<Employee> GetByPointId(string PointId)
        {
            string name = null;
            bool enabled = true;
            var query = repos.SQL.OrderByDesc(p => p.CreateTime);
            SetQuery(query, name, enabled, PointId);
            return repos.Fetch(query);
        }
        /// <summary>
        /// 获取启用的简单对象集合(已排序)
        /// </summary>
        public List<Simple> GetSimpleList()
        {
            var query = repos.SQL
                .Where(p => p.Enabled == true)
                .OrderByDesc(p => p.CreateTime)
                .Select(p => p.Id, p => p.Name);

            return repos.Fetch(query).
                Select(p => new Simple(p.Id, p.Name, SpellHelper.GetSpell(p.Name)))
                .ToList();
        }

        /// <summary>
        /// 获取启用的对象集合(已排序)
        /// </summary>
        /// <param name="name">名称关键字</param>
        /// <param name="enabled">有限</param>
        /// <param name="pointId">检查点</param>
        public List<Employee> GetList(string name, bool? enabled, string pointId)
        {
            var query = repos.SQL.OrderByDesc(p => p.CreateTime);
            SetQuery(query, name, enabled, pointId);
            return repos.Fetch(query);
        }

        /// <summary>
        /// 获取对象分页集合
        /// </summary>
        /// <param name="pageCondition">分页对象</param>
        /// <param name="name">名称关键字</param>
        /// <param name="enabled">有限</param>
        /// <param name="pointId">检查点</param>
        public PageList<Employee> GetPageList(PaginationCondition pageCondition,
            string name, bool? enabled, string pointId)
        {
            pageCondition.SetDefaultOrder(nameof(Employee.CreateTime));
            var query = repos.PageQuery(pageCondition);
            SetQuery(query, name, enabled, pointId);
            query.ExcludeSelect(p => p.Note);
            return repos.Page(query);
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="query">查询对象</param>
        /// <param name="search">数据对象</param>
        public void SetQuery(SQLQuery<Employee> query, string name, bool? enabled, string pointId)
        {
            if (enabled.HasValue)
            {
                query.Where(p => p.Enabled == enabled.Value);
            }
            if (pointId.IsNotEmpty())
            {
                pointId = pointId.Trim();
                query.Where(p => p.PointId == pointId);
            }
            if (name.IsNotEmpty())
            {
                name = name.Trim();
                query.Where(p => p.Name.Contains(name)
                    || p.Phone.Contains(name)
                    || p.UserAccount.Contains(name));
            }
        }
    }
}
